package com.zmn.plat.admin.controller.contract.payment.record;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.plat.common.dictionary.ContractPaymentStageEnum;
import com.zmn.plat.model.entity.contract.payment.record.ContractPaymentRecord;
import com.zmn.plat.model.entity.contract.payment.record.ContractPaymentRecordQuery;
import com.zmn.plat.model.vo.contract.payment.ContractPaymentRecordVO;

/**
 * 合同收付记录表
 *
 * @author linfeng
 *
 * @since 2020-05-09 11:41:45
 */
@Controller
@RequestMapping("/contract/payment/record/")
public class ContractPaymentRecordListController extends ContractPaymentRecordBaseController {

    /**
     * 列表
     * 
     * @author linfeng
     * @param query
     * @since 2020-05-09 11:41:45
     */
    @RequestMapping("list")
    @RequiresPermissions("/contract/payment/record/list.action")
    public ModelAndView list(@ModelAttribute ContractPaymentRecordQuery query) {

        List<ContractPaymentRecord> items = contractPaymentRecordService.listByQuery(query);

        ModelAndView mav = new ModelAndView("contract/payment/record/list");
        mav.addObject("items", items);
        mav.addObject("obj", query);
        mav.addObject("flag", 2);
        return mav;
    }

    /**
     * 详情
     * 
     * @author linfeng
     * @param relateId
     * @since 2020-05-09 11:41:45
     */
    @RequestMapping("detail")
    @RequiresPermissions("/contract/payment/record/detail.action")
    public ModelAndView detail(@RequestParam(value = "contractId", defaultValue = "0") Integer contractId,
        @RequestParam(value = "relateId", defaultValue = "0") Integer relateId) {
        ContractPaymentRecord contractPaymentRecord;
        if (relateId == null || relateId == 0) {
            contractPaymentRecord = new ContractPaymentRecord();
            contractPaymentRecord.setContractId(contractId);
        } else {
            contractPaymentRecord = contractPaymentRecordService.findByKey(relateId);
        }

        ContractPaymentRecordVO contractPaymentRecordVO = new ContractPaymentRecordVO();
        BeanUtils.copyProperties(contractPaymentRecord, contractPaymentRecordVO);
        contractPaymentRecordVO.setAmountYuan(narrow(contractPaymentRecordVO.getAmount()));

        ModelAndView mav = new ModelAndView("contract/payment/record/detail");
        mav.addObject("contractPaymentRecordVO", contractPaymentRecordVO);
        mav.addObject("contractPaymentStages", ContractPaymentStageEnum.values());
        return mav;
    }

}
